package com.ccibe.g16system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccibe.g16system.pojo.Goods;
import com.ccibe.g16system.service.GoodsService;
import jakarta.annotation.Resource;
import jakarta.websocket.server.PathParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @GetMapping("/goods")
    public String goods(Model model) {
        List<Goods> goodsList = goodsService.list();
        model.addAttribute("goodsList", goodsList);
        return "goods";
    }

    @GetMapping("/manage")
    public String manage(@PathParam("page") Integer page, @PathParam("pageSize") Integer pageSize, Model model) {
        Page<Goods> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        queryGoods(model, pageInfo, wrapper);
        return "manage";
    }

    private void queryGoods(Model model, Page<Goods> pageInfo, LambdaQueryWrapper<Goods> wrapper) {
        wrapper.eq(Goods::getState, 0);
        Page<Goods> goodsPage = goodsService.page(pageInfo, wrapper);
        model.addAttribute("currentPage", goodsPage.getCurrent());
        model.addAttribute("totalPage", goodsPage.getPages());
        model.addAttribute("total", goodsPage.getTotal());
        model.addAttribute("pageSize", goodsPage.getSize());
        model.addAttribute("goodsList", goodsPage.getRecords());
    }

    @GetMapping("/manage/search")
    public String search(@PathParam("page") Integer page, @PathParam("pageSize") Integer pageSize, @PathParam("keyword") String keyword, Model model) {
        Page<Goods> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Goods::getTitle, keyword);
        queryGoods(model, pageInfo, wrapper);
        return "manage::table";
    }

    @GetMapping("/upload")
    public String upload(Model model) {
        Goods goods = new Goods();
        model.addAttribute("goods", goods);
        return "upload";
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        Goods goods = goodsService.getById(id);
        if (goods == null) {
            map.put("success", false);
            return map;
        }
        goods.setState(1);
        boolean result = goodsService.updateById(goods);
        if (result) {
            map.put("success", true);
        } else {
            map.put("success", false);
        }
        return map;
    }

    @GetMapping("/upload/{id}")
    public String updateGoods(@PathVariable("id") Integer id, Model model) {
        Goods goods = goodsService.getById(id);
        if (goods == null) goods = new Goods();
        model.addAttribute("goods", goods);
        return "upload";
    }

    @PostMapping("/goods/add")
    public String addGoods(@ModelAttribute Goods goods, Model model) {
        boolean result = goodsService.addGoods(goods);
        if (result) {
            model.addAttribute("successTitle", "添加商品成功");
            model.addAttribute("successMessage", "商品添加成功");
            model.addAttribute("redirectUrl", "/manage?page=1&pageSize=5");
        } else {
            model.addAttribute("errorTitle", "添加商品失败");
            model.addAttribute("errorMessage", "商品添加失败");
            model.addAttribute("redirectUrl", "/upload");
            model.addAttribute("goods", goods);
        }
        return "redirect";
    }

    @PostMapping("/goods/edit")
    public String editGoods(@ModelAttribute Goods goods, Model model) {
        boolean result = goodsService.updateById(goods);
        if (result) {
            model.addAttribute("successTitle", "修改商品成功");
            model.addAttribute("successMessage", "商品修改成功");
            model.addAttribute("redirectUrl", "/manage?page=1&pageSize=5");
        } else {
            model.addAttribute("errorTitle", "修改商品失败");
            model.addAttribute("errorMessage", "商品修改失败");
            model.addAttribute("redirectUrl", "/upload");
            model.addAttribute("goods", goods);
        }
        return "redirect";
    }
}
